package com.accesshollywood.SQL;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.DurationFieldType;
import org.joda.time.Interval;
import org.joda.time.Months;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

import com.lavantech.gui.comp.DateTimePicker;

public class GrfxDateTime{
	public static final boolean	FUTURE	= true;
	public static final boolean	PAST	= false;

	public DateTime				rawDateTime;

	public GrfxDateTime() {
		rawDateTime = new DateTime();
		//Trace.info(rawDateTime.toString());
	}
	
	public GrfxDateTime(Date date){
		rawDateTime = new DateTime(date);
	}

	public GrfxDateTime(long milliseconds) {
		rawDateTime = new DateTime(milliseconds);
		//Trace.info(rawDateTime.toString());
	}

	public GrfxDateTime(DateTimePicker dateTimePicker) {
		rawDateTime = new DateTime(dateTimePicker.getDate());
		//Trace.info(rawDateTime.toString());
	}

	public GrfxDateTime(String rawDateTimeStr) {
		if (rawDateTimeStr != null) {
			String[] raw = rawDateTimeStr.split("\\ ");
			String[] rawDate = raw[0].split("\\-");
			String[] rawTime = raw[1].split("\\:");

			int year = Integer.parseInt(rawDate[0]);
			int month = Integer.parseInt(rawDate[1]);
			int day = Integer.parseInt(rawDate[2]);

			int hour = Integer.parseInt(rawTime[0]);
			int minute = Integer.parseInt(rawTime[1]);
			int second = Integer.parseInt(rawTime[2].split("\\.")[0]);

			rawDateTime = new DateTime(year, month, day, hour, minute, second);
		} else {
			rawDateTime = new DateTime();
		}
	}

	public boolean isPast() {
		return rawDateTime.isBeforeNow();
	}

	public boolean isFuture() {
		return rawDateTime.isAfterNow();
	}

	public String toSQL() {
		// YYYY-MM-DD HH:mm:ss
		return rawDateTime.getMillis() + "";
		// return rawDateTime.toString("YYYY-MM-dd HH:mm:ss");
	}

	public String toString() {
		DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm a  MM/dd/yyyy");
		String str = fmt.print(rawDateTime);
		return str;
	}

	public String toRemainingString() {
		DateTime now = new DateTime();

		Duration dur = new Duration(rawDateTime, now);
		DurationFieldType[] types =
		{
				DurationFieldType.years(),
				DurationFieldType.months(),
				DurationFieldType.days(),
				DurationFieldType.hours(),
				DurationFieldType.minutes()
		};
		PeriodType periodType = PeriodType.forFields(types);
		Period p;
		if (rawDateTime.isBefore(now)) {
			p = new Interval(rawDateTime, now).toPeriod(periodType);
		} else {
			p = new Interval(now, rawDateTime).toPeriod(periodType);
		}
		PeriodFormatter formatter;

		PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder().minimumPrintedDigits(2);

		//Trace.info(Years.yearsBetween(rawDateTime, now));
		//Trace.info(Months.monthsBetween(rawDateTime, now));
		//Trace.info(Days.daysBetween(rawDateTime, now));

		if (!Years.yearsBetween(rawDateTime, now).isLessThan(Years.ONE) || !Years.yearsBetween(now, rawDateTime).isLessThan(Years.ONE)) {
			formatterBuilder.appendYears()
					.appendSeparator("Yr ")
					.appendMonths()
					.appendSeparator("M ")
					.appendDays()
					.appendLiteral("D ");
		} else if (!Months.monthsBetween(rawDateTime, now).isLessThan(Months.ONE) || !Months.monthsBetween(now, rawDateTime).isLessThan(Months.ONE)) {
			formatterBuilder.appendMonths()
					.appendSeparator("M ")
					.appendDays()
					.appendLiteral("D ")
					.appendHours()
					.appendLiteral("Hrs ");
		} else {
			formatterBuilder.appendDays()
					.appendSeparator("D ")
					.appendHours()
					.appendSeparator("Hrs ")
					.appendMinutes()
					.appendLiteral("Mins ");
		}

		formatter = formatterBuilder.toFormatter();

		String suffix = " left";
		if (isPast()) {
			suffix = " LATE";
		}

		return formatter.print(p).replaceAll("\\-", "") + suffix;
	}

	public String zero(int n) {
		String n2 = "0000" + n;
		return n2.substring(n2.length() - 2, n2.length());
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((rawDateTime == null) ? 0 : rawDateTime.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GrfxDateTime other = (GrfxDateTime) obj;
		if (rawDateTime == null) {
			if (other.rawDateTime != null)
				return false;
		} else if (!rawDateTime.equals(other.rawDateTime))
			return false;
		return true;
	}


}
